using System;
using System.ComponentModel;
using System.ComponentModel.Design.Serialization;
using System.Globalization;
using System.Drawing;
using System.Reflection;

namespace WebChart.Design
{
	public class StringFormatConverter : ExpandableObjectConverter
	{
		// Methods
		public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
		{
			return ((sourceType == typeof(string)) || base.CanConvertFrom(context, sourceType));
		}

		public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
		{
			if ((destinationType != typeof(string)) && (destinationType != typeof(InstanceDescriptor)))
			{
				return base.CanConvertTo(context, destinationType);
			}
			return true;
		}

		public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
		{
			if (value == null)
			{
				return new StringFormat();
			}
			if (!(value is string))
			{
				return base.ConvertFrom(context, culture, value);
			}
			string text = (string) value;
			if ((text == null) || (text.Length == 0))
			{
				return new StringFormat();
			}
			StringFormat format = new StringFormat();
			string[] textArray = null;
			if (culture != null)
			{
				textArray = text.Split(culture.TextInfo.ListSeparator.ToCharArray());
			}
			else
			{
				textArray = text.Split(CultureInfo.InvariantCulture.TextInfo.ListSeparator.ToCharArray());
			}
			if ((textArray == null) || (textArray.Length != 4))
			{
				return new StringFormat();
			}
			EnumConverter converter = new EnumConverter(typeof(StringAlignment));
			format.Alignment = (StringAlignment) converter.ConvertFromString(textArray[0]);
			format.LineAlignment = (StringAlignment) converter.ConvertFromString(textArray[1]);
			EnumConverter converter2 = new EnumConverter(typeof(StringTrimming));
			format.Trimming = (StringTrimming) converter2.ConvertFromString(textArray[2]);
			EnumConverter converter3 = new EnumConverter(typeof(StringFormatFlags));
			format.FormatFlags = (StringFormatFlags) converter3.ConvertFromString(textArray[3]);
			return format;
		}

		public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
		{
			if ((value != null) && !(value is StringFormat))
			{
				throw new ArgumentException("Invalid StringFormat", "value");
			}
			if (destinationType == typeof(string))
			{
				if (value == null)
				{
					return string.Empty;
				}
				StringFormat format = (StringFormat) value;
				if (culture != null)
				{
					return string.Join(culture.TextInfo.ListSeparator, new string[] { format.Alignment.ToString(), format.LineAlignment.ToString(), format.Trimming.ToString(), format.FormatFlags.ToString() });
				}
				return string.Join(CultureInfo.InvariantCulture.TextInfo.ListSeparator, new string[] { format.Alignment.ToString(), format.LineAlignment.ToString(), format.Trimming.ToString(), format.FormatFlags.ToString() });
			}
			if (destinationType != typeof(InstanceDescriptor))
			{
				return base.ConvertTo(context, culture, value, destinationType);
			}
			if (value != null)
			{
				MemberInfo member = null;
				object[] arguments = null;
				StringFormat format2 = (StringFormat) value;
				member = typeof(StringFormatConverter).GetMethod("CreateStringFormat", new Type[] { typeof(StringAlignment), typeof(StringAlignment), typeof(StringTrimming), typeof(StringFormatFlags) });
				arguments = new object[] { format2.Alignment, format2.LineAlignment, format2.Trimming, format2.FormatFlags };
				if (member != null)
				{
					return new InstanceDescriptor(member, arguments);
				}
			}
			return null;
		}

		public static StringFormat CreateStringFormat(StringAlignment alignment, StringAlignment lineAlignment, StringTrimming trimming, StringFormatFlags formatFlags)
		{
			StringFormat format = new StringFormat(formatFlags);
			format.Alignment = alignment;
			format.LineAlignment = lineAlignment;
			format.Trimming = trimming;
			return format;
		}
	}
}
